
//  路由配置
import { createRouter,createWebHashHistory } from "vue-router";
import Layout from "@/views/Layout/Layout.vue";
import Home from '@/views/Home/Home.vue'
import useStore from "@/store";
import Message from "@/components/XtxMessage";
const router = createRouter({
  // 设置页面跳转的位置
  scrollBehavior:() => {
    return { top: 0 }
  },
  history:createWebHashHistory(),
  routes:[
    {
      path:'/',
      component:Layout,
      children:[
        {
          path: '',
          component: Home
        },
        {
          // 动态路由
          // category/123 ===>
          // category/abcd ===>
          path: 'category/:id',
          component:() => import('@/views/Category/Category.vue')
        },
        {
          //详情页
          path: 'product/:id', //默认的显示内容
          component:() => import('@/views/Goods/Goods.vue')
        },
        {
          // 登录页
          path: '/cart',
          component: () => import('@/views/Cart/Cart.vue')
        },
        // 支付页
        {
          path: '/member/checkout',
          component: () => import('@/views/member/pay/checkout.vue')
        },
        // 支付订单
        {
          path: '/member/pay',
          component: () => import('@/views/Member/Pay.vue')
        }
      ]
    },
    {
      path: '/login',
      component: () => import('@/views/Login/Login.vue')
    },
    {
      path: '/login/callback',
      component: () => import('@/views/login/callback.vue')
    },
    {
      path: '/play',
      component: () => import('@/views/Playground.vue')
    }
  ]
})
router.beforeEach((to,from,next)=>{
  const { cart } = useStore()
  // to 去哪个页面
  // 没有登录的用户,就不能访问 '/member/checkout'
  if(cart.isLogin === false  &&  to.path === '/member/checkout') {
    Message.warning('没有登录,不能访问')
    const url = '/login?redirect=' + to.fullPath
    next(url)
  }else {
    next()
  }
})


export default router